//
//  CZMenuButton.m
//  QQ空间
//
//  Created by zhangping on 16/1/19.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZMenuButton.h"
#import "CZMenuItem.h"

@implementation CZMenuButton

+ (instancetype)buttonWithMenuItem:(CZMenuItem *)menuItem {
    return [[self alloc] initWithMenuItem:menuItem];
}

- (instancetype)initWithMenuItem:(CZMenuItem *)menuItem {
    if (self = [super init]) {
        _menuItem = menuItem;
        
        [self prepareUI];
    }
    
    return self;
}

/// 准备UI
- (void)prepareUI {
    // 根据模型设置按钮
    [self setImage:[UIImage imageNamed:self.menuItem.imageName] forState:UIControlStateNormal];
    
    // 设置背景图片
    [self setBackgroundImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateSelected];
    
    self.adjustsImageWhenHighlighted = NO;
    
    // 不是撰写按钮不需要设置标题
    if (!self.menuItem.isComposeButton) {
        [self setupMenuButton:YES];
        
        // 添加底部分割线
        UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_separate_line"]];
        [self addSubview:separatorView];
        
        [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(self);
            make.height.equalTo(@2);
        }];
    } else {
        // 添加右侧分割线
        UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_separate_ugc_line_v"]];
        [self addSubview:separatorView];
        
        [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.trailing.bottom.equalTo(self);
            make.width.equalTo(@2);
        }];
    }
}

/// 根据横竖屏来设置菜单按钮的边距和标题
- (void)setupMenuButton:(BOOL) isLandscape {
    if (isLandscape) {
        // 设置标题
        [self setTitle:self.menuItem.title forState:UIControlStateNormal];
        
        // 设置内容 水平左对齐
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        // 设置 内容的内边距
        self.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        
        // 设置 标题的内边距
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    } else {
        [self setTitle:nil forState:UIControlStateNormal];
        // 设置内容 水平左对齐
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        
        // 设置 内容的内边距
        self.contentEdgeInsets = UIEdgeInsetsZero;
        
        // 设置 标题的内边距
        self.titleEdgeInsets = UIEdgeInsetsZero;
    }
}
@end
